<?php
class admin_module_user
{
    private $template;
    private $util;
    private $db;
    
    public function __construct(){ 
        require_once("../code/db.class.php");
        $this->util = new util();
        $this->db = new db();
        
        
    }
    
    public function autorun(){
         $act = isset($_GET['act'])?$_GET['act']:'list';
         
         switch($act){
            case 'list':
                $this->list_user();
                break;
            case 'create':
                $this->create_user();
                break;
            case 'update';
                $this->update_user();
                break;
            case 'delete':
                $this->do_delete_user();
                break;
            default:
                $this->list_user();
                break;
         }     
    } 
    //List user
    private function list_user(){
        //Load data
    
        $str = "SELECT * FROM admin";
        $this->db->openconnection();
        $data = $this->db->run_query($str);
        $this->db->closeconnection();
        //Send data to template

        $this->util->send_var('module','list_table');
        $this->util->send_var('data',$data);
        $this->util->generate_page(ADMIN_TEMPLATES.'/user/index.tpl');  
    } 
    
     //Create 
    private function create_user(){
        //Send data to template
        $sendby=isset($_POST['sendby'])?$_POST['sendby']:'';  
        if($sendby==''){
            $this->util->send_var('module','create_form');
            $this->util->generate_page(ADMIN_TEMPLATES.'/user/index.tpl');
        }
        else
        {
            $username = isset($_POST['username'])?strval($_POST['username']):'';
            $fullname = isset($_POST['fullname'])?strval($_POST['fullname']):'';  
            $password = isset($_POST['password'])?strval($_POST['password']):'';   
            
            $str = "INSERT INTO admin(username, fullname, password) VALUES ('". $username ."','". $fullname ."','". md5($password) ."')";
            $this->db->openconnection();
            $newid=$this->db->run_insert_query($str);
            $this->db->closeconnection();
            if($newid!=null){
                $this->list_user();    
            }
            
        }
    }
    //Update 
    private function update_user(){
        $id=isset($_GET['id'])?$_GET['id']:0;
        if($id==0){
            //Lỗi: không có giá trị 0 trong csdl
            exit();    
        }
        
        $sendby=isset($_POST['sendby'])?$_POST['sendby']:'';  
        if($sendby=='update'){
            $username = isset($_POST['username'])?strval($_POST['username']):'';
            $fullname = isset($_POST['fullname'])?strval($_POST['fullname']):'';  
            
            $str = "UPDATE admin SET username = '". $username ."', fullname = '". $fullname ."' WHERE id=". $id;
            $this->db->openconnection();
            $newid=$this->db->run_query($str);
            $this->db->closeconnection();
           
            $this->list_user();    
     
        }
        else
        {
            $this->db->openconnection();
            $data = $this->db->run_select_by_id('admin',$id);
            $this->db->closeconnection();
           
            //Send data to template
            $this->util->send_var('module','update_form');
            $this->util->send_var('data',$data);
            $this->util->generate_page(ADMIN_TEMPLATES.'/user/index.tpl');
        }
    }
     //Do Create new calendar
    private function do_delete_user(){
        //take input
        $id= isset($_GET['id'])?intval($_GET['id']):0; 
        if($id==0){
            header('Location:index.php');   
        }
        //Insert database
        $str = "DELETE FROM admin WHERE id = ". $id;   
          
        //print($str);
        //exit();
        $this->db->openconnection();
        $id = $this->db->run_query($str);
        $this->db->closeconnection();
        //Oke
        $this->list_user();      
    }
}  
?>
